package com.example.demo.queue.producer;

import com.example.demo.queue.message.TestEvent;
import com.lmax.disruptor.RingBuffer;
import lombok.RequiredArgsConstructor;
import org.springframework.cglib.beans.BeanCopier;

@RequiredArgsConstructor
public class TestEventProducer {
    private final RingBuffer<TestEvent> ringBuffer;

    public void startTest(TestEvent obj) {
        long sequence = ringBuffer.next();
        try {
            TestEvent event = ringBuffer.get(sequence);
            BeanCopier.create(TestEvent.class, TestEvent.class, false).copy(obj, event, null);
        } finally {
            // 发布事件到队列中
            ringBuffer.publish(sequence);
        }
    }
}
